import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';
import 'package:test/test.dart';

import 'package:sow_app_mock/sow_app_mock.dart';

void main() {
  group('novel detail controller test group', () {
    final MockNovelDetailPresenter presenter = MockNovelDetailPresenter();
    final CacheRepository cache = MockCacheRepository();
    presenter.novelId = "1";
    presenter.novel = Novel(
        novelId: '1',
        title: 'hello',
        author: 'world',
        novelTypeList: [],
        cover: I18n.base_cover_url);
    presenter.chapterList.add(
        Chapter(chapterId: "1", novelId: "1", catalogue: '1', content: '1'));
    final controller = MockNovelDetailController(presenter);
    test("should present valid chapter list data when invoke getChapterList",
        () async {
      await controller.init();
      await Future.doWhile(() => presenter.flag = false);
      expect(presenter.chapterList, isNotEmpty);
    });
    test("should present valid subscribe info data when invoke subscribe",
        () async {
      await controller.subscribe();
      await Future.doWhile(() => presenter.flag = false);
      expect(presenter.inShelf, true);
    });
    test("should present valid unsubscribe info data when invoke subscribe",
        () async {
      await controller.subscribe();
      await Future.doWhile(() => presenter.flag = false);
      expect(presenter.inShelf, false);
    });
    test("should exec normal info data when invoke addReadHistory", () async {
      await controller.addReadHistory();
      await Future.doWhile(() => presenter.flag = false);
    });
    test(
        "should present valid chapter list data when invoke init with all zero cache",
        () async {
      await cache.set(
          '${presenter.chapterList.first.novelId}_${presenter.chapterList.first.catalogue}',
          '666');
      await cache.set(
          '${presenter.chapterList.first.novelId}_${presenter.chapterList.first.catalogue}_%',
          '0.0,0.0,0.0');
      await controller.init();
      await Future.doWhile(() => presenter.flag = false);
      expect(presenter.chapterList, isNotEmpty);
    });
    test(
        "should present valid chapter list data when invoke  init with not all zero cache",
        () async {
      await cache.set(
          '${presenter.chapterList.first.novelId}_${presenter.chapterList.first.catalogue}',
          '666');
      await cache.set(
          '${presenter.chapterList.first.novelId}_${presenter.chapterList.first.catalogue}_%',
          '0.0,0.2,0.0');
      await controller.init();
      await Future.doWhile(() => presenter.flag = false);
      expect(presenter.chapterList, isNotEmpty);
    });
  });
}
